#include "mainwidget.h"

#include <QApplication>
#include <QMutex>
#include "model/data.h"
#include "model/datacenter.h"

static QMutex fileMutex;
FILE* output = nullptr;

void logToFile(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
    QString date = QDateTime::currentDateTime().toString("yyyy/MM/dd hh:mm:ss");
    QMutexLocker locker(&fileMutex); // 自动加锁
    fprintf(output, "[%s] %s\n", date.toUtf8().constData(), msg.toUtf8().constData());
    fflush(output);
}

int main(int argc, char *argv[])
{
#if TEST_DEPLOY // 日志重定向
    output = fopen("./log.txt", "a");
    qInstallMessageHandler(logToFile); // 日志回调函数，返回值为原有函数指针
#endif

    QApplication a(argc, argv);


#if TEST_LOGIN
    LoginWidget* logIn = new LoginWidget();
    logIn->show();
#else
    MainWidget* w = MainWidget::getInstance();
    w->show();
#endif

#if TEST_DATACENTER_COMPONENT // 测试数据控制类
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    dataCenter->initDataFile();
    dataCenter->saveDataFile();
    dataCenter->loadDataFile();
#endif

#if TEST_CONNECT_SERVER // 测试服务器连通性
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    dataCenter->ping();
#endif
    return a.exec();
}
